//
//  YYMeetDetailViewController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/14.
//  Copyright © 2017年 stoneobs. All rights reserved.
// //**********************遇见详情***************************

import UIKit

class YYMeetDetailViewController: STTableViewController {

    //ui
    var bannerImageView:UIImageView?
    
    var playButton:STButton?
    
    var desLable:STLabel?
    
    var trakView:Array<UIView> = [];
    
    
    
    var model:YYMeetModel = YYMeetModel(){
        
        didSet{
            
        }
    };
    deinit {
        print("YYMeetDetailViewController 释放了");
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.height = SCREEN_HEIGHT - 64;
        self.tableView.separatorStyle = .none;
        self.tableView.backgroundColor = UIColor.white;
        self.view.backgroundColor = .white;
        self.configSubView();
    }
    
    func configSubView(){
        self.view.backgroundColor = .white;
        
        let tableHeaderView = UIView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: 40));
        self.title = self.model.username;
        weak var weakSelf = self;
        
        self.bannerImageView = UIImageView.init(frame: .init(x: 0, y: 10, width:2 * SCREEN_WIDTH / 3, height:2 *  SCREEN_WIDTH / 3));
        self.bannerImageView?.sd_setImage(with: URL.init(string: (self.model.playlist_image)!), placeholderImage: BannerImage, options: .retryFailed);
        self.bannerImageView?.contentMode = .scaleAspectFill;
        self.bannerImageView?.centerX = SCREEN_WIDTH / 2;
        self.bannerImageView?.clipsToBounds = true;
        tableHeaderView.addSubview(self.bannerImageView!);
        
        let titleLable:STLabel = STLabel.init(frame: .init(x: SCREEN_WIDTH / 2 - 140, y: 10, width: SCREEN_WIDTH - 50, height: 17),
                                              text: self.model.playlist_name,
                                              textColor: FirstTextColor,
                                              font: 17,
                                              isSizetoFit: false,
                                              textAlignment: .right);
        
        titleLable.top = (self.bannerImageView?.bottom)! + 40;
        titleLable.centerX = SCREEN_WIDTH / 2;
        titleLable.textAlignment = .center;
        tableHeaderView.addSubview(titleLable);
        
        
        let iconImageView:UIImageView = UIImageView.init(frame: .init(x: 0, y: 0, width: 20, height: 20));
        iconImageView.layer.cornerRadius = 10;
        iconImageView.clipsToBounds = true;
        iconImageView.centerY = titleLable.bottom + 20;
        iconImageView.centerX = SCREEN_WIDTH / 2;
        iconImageView.sd_setImage(with: URL.init(string: model.pimg!), placeholderImage: PeopleImage, options: .retryFailed);
        tableHeaderView.addSubview(iconImageView);
        
        let nickNameLable = STLabel.init(frame: .init(x: SCREEN_WIDTH / 2 - 150, y: 10, width: 130, height: 13),
                                         text: model.username,
                                         textColor: ThirdTextColor,
                                         font: 12,
                                         isSizetoFit: false,
                                         textAlignment: .right);
        nickNameLable?.top = iconImageView.bottom + 10;
        let str:NSString = NSString.init(string: model.username!);
        nickNameLable?.width = str.widthWithheight(12, font: 12);
        if (nickNameLable?.width)! > CGFloat(200.0) {
            nickNameLable?.width = 200.0;
        }
        nickNameLable?.centerX = SCREEN_WIDTH / 2;
        tableHeaderView.addSubview(nickNameLable!);
        //男 女
        let genderImageView:UIImageView = UIImageView.init(frame: .init(x: 0, y: 0, width: 12, height: 12));
        genderImageView.centerY = (nickNameLable?.centerY)!;
        genderImageView.left = (nickNameLable?.right)! + 5;
        if model.genter == "男" {
            genderImageView.image = UIImage.init(named: "img_boy");
            
        }else{
            genderImageView.image = UIImage.init(named: "img_girl");
        }
        tableHeaderView.addSubview(genderImageView);
        
        
        let line:UIView = UIView.init(frame: CGRect.init(x: 0, y: (nickNameLable?.bottom)! + 70, width: SCREEN_WIDTH / 2, height: 1));
        line.backgroundColor = LINE_COLOR;
        line.centerX = SCREEN_WIDTH / 2;
        tableHeaderView.addSubview(line);
        
        self.playButton = STButton.init(frame: .init(x: 0, y: 0, width: 32, height: 32),
                                        title: "",
                                        titleColor: nil,
                                        titleFont: 0,
                                        cornerRadius: 0,
                                        backgroundColor: .clear,
                                        backgroundImage: nil,
                                        image: UIImage.init(named: ""));
        
        
        self.playButton?.setBackgroundImage(UIImage.init(named: "btn_play"), for: .normal);
        self.playButton?.setBackgroundImage(UIImage.init(named: "btn_pause"), for: .selected);
        self.playButton?.contentHorizontalAlignment = .center;
        self.playButton?.centerX = SCREEN_WIDTH / 2;
        self.playButton?.centerY = line.centerY;
        self.playButton?.clicAction = {but in
            
            weakSelf?.playButton?.isSelected = !(weakSelf?.playButton?.isSelected)!;
        }
        tableHeaderView.addSubview(self.playButton!);
        
        
        let numLable:STLabel = STLabel.init(frame: .init(x: SCREEN_WIDTH / 2 - 150, y: 10, width: 130, height: 13),
                                            text: "173期",
                                            textColor: ThirdTextColor,
                                            font: 12,
                                            isSizetoFit: false,
                                            textAlignment: .right);
        numLable.text = "共"  + String((model.trackModels?.count)!) + "首";
        numLable.top = line.bottom + 30;
        tableHeaderView.addSubview(numLable);
        
        let listenButton =  STButton.init(frame: CGRect.init(x: SCREEN_WIDTH / 2  + 20, y: 0, width: 70, height: 14),
                                          title: self.model.play_count,
                                          titleColor: SecendTextColor,
                                          titleFont: 13,
                                          cornerRadius: 0,
                                          backgroundColor: .clear,
                                          backgroundImage: nil,
                                          image: UIImage.init(named: "btn_listen"));
        listenButton?.contentHorizontalAlignment = .left;
        listenButton?.centerY = numLable.centerY;
        tableHeaderView.addSubview(listenButton!);
        
        
        
        tableHeaderView.height = (listenButton?.bottom)! + 40;
        
        self.tableView.tableHeaderView = tableHeaderView;
        
        
    }
    // MARK: UITableView DataSouce
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model.trackModels!.count;
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YYFindMusicDetailTableViewCell.cellHeight;
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0001;
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:YYFindMusicDetailTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell") as? YYFindMusicDetailTableViewCell;
        if cell == nil {
            cell = YYFindMusicDetailTableViewCell.init(style: .default, reuseIdentifier: "cell");
        }
        
        let model:YYTrack = self.model.trackModels![indexPath.row];
        
        model.indexString = String(indexPath.row + 1);
        
        
        cell?.selectionStyle = .none
        cell?.model = model;
        return cell!;
    }
    
    // MARK: UITableView Delegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false);
        let trackModel:YYTrack = self.model.trackModels![indexPath.row];
        
        for oneModel in self.model.trackModels! {
            
            if oneModel.id != model.id {
                oneModel.playType = .nomal;
            }
            
        }
        
        
        var type:YYTrackModelType = .nomal;
        if trackModel.playType == .nomal {
            type = .isPlaying;
            let musicModel:YYMusicModel = YYMusicModel();
            musicModel.resouceUrl = trackModel.filename;
            YYMusicPlayManger.defultManger.playWithModel(model: musicModel);
            
        }else if trackModel.playType == .isPlaying {
            type = .pause;
            YYMusicPlayManger.defultManger.pause();
        }else{
            type = .isPlaying;
            YYMusicPlayManger.defultManger.play();
        }
        trackModel.playType = type;
        
        self.tableView.reloadData();
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
